package com.binsoft.ndtta;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Match 
{
	@SuppressWarnings("unused")
	private static final String TAG = "Match";
	private int mHome = -1;
	private int mAway = -1;
	private int mWeek = 0;
	private long mDate = 0;
	private int mHomeScore = 0;
	private int mAwayScore = 0;
	private int mDivision = 0;
	private String mDescription = "";
	
	Match( int week, String description )
	{
		mWeek = week;
		mDescription = description;
	}
	
	Match( int week, int home, int away )
	{
		mHome = home;
		mAway = away;
		mWeek = week;
	}

	public String description()
	{
		return mDescription;
	}
	
	public void division( int division )
	{
		mDivision = division;
		
		// If this is the first call, set the date.
		if ( mDate == 0 )
		{
			GregorianCalendar cal = (GregorianCalendar)Constants.START_DATE.clone();
			
			cal.add( Calendar.WEEK_OF_YEAR, mWeek );
			
			// Now work out the difference between home night and the Monday.
			Team homeTeam = Matches.getTeam( mDivision, mHome );
			Team awayTeam = Matches.getTeam( mDivision, mAway );
	
			int homeNight = Calendar.MONDAY;
			if ( ( homeTeam != null && !homeTeam.name().isEmpty() ) || 
					( awayTeam != null && !awayTeam.name().isEmpty() ) ) 
	 		{
				homeNight = homeTeam.homeNight();
			}
	
			cal.add( Calendar.DAY_OF_WEEK, homeNight - Calendar.MONDAY);
			
			mDate = cal.getTime().getTime();
		}
	}
	
	public int division()
	{
		return mDivision;
	}
	
	public long date()
	{
		return mDate;
	}
	
	public void homeScore( int score )
	{
		mHomeScore = score;
	}
	
	public int homeScore()
	{
		return mHomeScore;
	}
	
	public void awayScore( int score )
	{
		mAwayScore = score;
	}
	
	public int awayScore()
	{
		return mAwayScore;
	}
	
	public int home()
	{
		return mHome;
	}
	
	public int away()
	{
		return mAway;
	}

	public int week()
	{
		return mWeek;
	}
	
	public boolean involves(int team) 
	{
		return ( ( mHome == team ) || ( mAway == team ) || ( mHome == -1 && mAway == -1 ) );
	}
}
